package com.briup.srms.exam.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.briup.srms.exam.bean.Paper;
import com.briup.srms.exam.bean.PaperQuestion;
import com.briup.srms.exam.bean.PaperQuestionExample;
import com.briup.srms.exam.bean.extend.PaperExtend;
import com.briup.srms.exam.bean.extend.QuestionWithScoreExtend;
import com.briup.srms.exam.dao.PaperMapper;
import com.briup.srms.exam.dao.PaperQuestionMapper;
import com.briup.srms.exam.dao.extend.PaperExtendMapper;
import com.briup.srms.exam.service.IPaperService;
import com.jacky.base.utils.CustomerException;
import com.jacky.base.utils.PageVM;

@Service
public class PaperServiceImpl implements IPaperService {
	@Autowired
	private PaperMapper paperMapper;
	@Autowired
	private PaperExtendMapper paperExtendMapper;
	@Autowired
	private PaperQuestionMapper paperQuestionMapper;

	@Override
	public PaperExtend findByIdWithoutAnswer(long id) {
		return paperExtendMapper.selectByIdWithoutAnswer(id);
	}

	@Override
	@Transactional
	public void edit(PaperExtend paper) {
		// 修改题目分数
		List<QuestionWithScoreExtend> questions = paper.getQuestions();
		if(questions!=null) {
			for(QuestionWithScoreExtend question : questions) {
				PaperQuestionExample example = new PaperQuestionExample();
				example.createCriteria().andPaperIdEqualTo(paper.getId()).andQuestionIdEqualTo(question.getId());
				List<PaperQuestion> list = paperQuestionMapper.selectByExample(example);
				if(list.size()>0) {
					PaperQuestion pq = list.get(0);
					pq.setScore(question.getQuestionScore());
					paperQuestionMapper.updateByPrimaryKey(pq);
				}
			}
		}
		// 修改试卷名称
		Paper p = paperMapper.selectByPrimaryKey(paper.getId());
		p.setName(paper.getName());
		System.out.println(p.getName());
		paperMapper.updateByPrimaryKey(p);
	}

	@Override
	public PaperExtend findById(long id) {
		return paperExtendMapper.selectById(id);
	}

	@Override
	public void merge(PaperExtend paper) throws CustomerException {
		if(!paper.getStatus().equals("未组卷")) {
			throw new CustomerException("试卷状态不合法");
		}
		Paper db_paper = paperMapper.selectByPrimaryKey(paper.getId());

		if(db_paper!=null && !db_paper.getName().equals(paper.getName())) {
			db_paper.setName(paper.getName());
			paperMapper.updateByPrimaryKey(db_paper);
		}
		List<QuestionWithScoreExtend> questions = paper.getQuestions();
		if(questions != null) {
			Double score_total = 0.0D;
			for(QuestionWithScoreExtend question : questions) {
				// 维护多表关系
				PaperQuestion qp = new PaperQuestion();
				qp.setPaperId(paper.getId());
				qp.setQuestionId(question.getId());
				qp.setScore(question.getScore());
				// 总分
				score_total += question.getScore();

				paperQuestionMapper.insert(qp);
				// 核对数量
				switch (question.getType()) {
					case "单选":
						paper.setNumRadio(paper.getNumRadio()+1);
						break;
					case "多选":
						paper.setNumCheck(paper.getNumCheck()+1);
						break;
					case "问答":
						paper.setNumAnswer(paper.getNumAnswer()+1);
						break;
					case "判断":
						paper.setNumJudge(paper.getNumJudge()+1);
						break;
					case "填空":
						paper.setNumCompletion(paper.getNumCompletion()+1);
						break;
					default:
						break;
				}
				paper.setNumTotal(paper.getNumTotal()+1);
			}
			paper.setScoreTotal(score_total);
		}
		// 更新状态
		paper.setStatus("已完成");
		paperMapper.updateByPrimaryKey(paper);
	}

	@Override
	public PageVM<Paper> pageQuery(int page, int pageSize) {
		List<Paper> list = paperExtendMapper.query(page, pageSize);
		long total = paperExtendMapper.count();
		return new PageVM<>(page, pageSize, total, list);
	}

	@Override
	public void saveOrUpdate(Paper paper) {
		if(paper.getId() == null) {
			paper.setStatus("未组卷");
			paper.setNumAnswer(0);
			paper.setNumCheck(0);
			paper.setNumCompletion(0);
			paper.setNumJudge(0);
			paper.setNumRadio(0);
			paper.setNumTotal(0);
			paper.setCreateTime(new Date().getTime());
			paper.setScoreTotal(0.0D);
			paperMapper.insert(paper);
		} else {
			paper.setUpdateTime(new Date().getTime());
			paperMapper.updateByPrimaryKey(paper);
		}
	}

	@Override
	public void deleteById(long id) {
		// TODO Auto-generated method stub
		Paper dict = paperMapper.selectByPrimaryKey(id);
		if(dict == null) {
			throw new CustomerException("要删除的数据不存在");
		}
		paperMapper.deleteByPrimaryKey(id);
	}
}
